﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CRConsult.Models
{
    public class newsRepository
    {
        /// <summary>
        /// 按category获取新闻列表
        /// </summary>
        /// <param name="id">category id</param>
        /// <returns></returns>
        public List<tb_News> getNewsListByCategory(int id) 
        {          
            using (LingHealEntities db = new LingHealEntities()) {
                IEnumerable<tb_News> items = db.tb_News;
                if (id != 0)
                    items = items.Where(i => i.CatID == id);
                return items.ToList();
            }
        }

        /// <summary>
        /// 新闻删除
        /// </summary>
        /// <param name="id"></param>
        public void delNews(int id)
        {
            using (LingHealEntities db = new LingHealEntities())
            {
                db.DeleteObject(db.tb_News.Where(i => i.NewsID == id));
                db.SaveChanges();
            }
        }

        /// <summary>
        /// 新闻更新
        /// </summary>
        /// <param name="item"></param>
        public void updateNews(tb_News item) {
            using (LingHealEntities db = new LingHealEntities()) {
                var originitem = db.tb_News.FirstOrDefault(i => i.NewsID == item.NewsID);
                originitem.NewsAuthor = item.NewsAuthor;
                originitem.NewsContent = item.NewsContent;
                originitem.NewsRelDate = DateTime.Now;
                originitem.NewsTitle = item.NewsTitle;
                originitem.IsTop = item.IsTop;
                db.SaveChanges();
            }
        }

        /// <summary>
        /// 新闻添加
        /// </summary>
        /// <param name="item"></param>
        public void insertNews(tb_News item)
        {
            using (LingHealEntities db = new LingHealEntities())
            {
                db.AddTotb_News(item);
                db.SaveChanges();
            }
        }
    }
}